package com.jcl.recruit.console.registerService;

import com.jcl.recruit.console.user.logic.ConsoleUserLogic;
import com.jcl.recruit.console.user.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RegisterService
 * @Author yyliu
 * @Date 2018/12/20 9:31
 * @Version 1.0
 **/
@Service
public class RegisterService {

    @Autowired
    private ConsoleUserLogic consoleUserLogic;

    public String register(SysUser user) {
        String username = user.getUsername();
        if (consoleUserLogic.findOne(username) != null) {
            return "用户已存在";
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = user.getPassword();
        user.setPassword(encoder.encode(rawPassword));
        List<String> roles = new ArrayList<>();
        roles.add("ROLE_USER");
        //user.setRoles(roles);
        //userRepository.insert(user);
        consoleUserLogic.insert(user);
        return "success";
    }
}
